export const noop = (): void => {}

export const isNotExist = (value: any): boolean => [null, undefined].includes(value)

export const replaceNotExist = (value: any, replace = ''): any => [null, undefined].includes(value) ? replace : value

export const isEmpty = (value: any): boolean => [null, undefined, ''].includes(value)

export const replaceEmpty = (value: any, replace = '-', callback?: Function): any => {
  return [null, undefined, ''].includes(value) ? replace : (callback ? callback(value) : value ) 
}

export const handleArrToStr = (arr: any[] = [], key = '', separatrix = ',', replace = '-'): string => {
  if (!arr || !Array.isArray(arr)) {
    return replace
  }
  const newArr = []
  arr.forEach((el) => {
    if (!isEmpty(key ? el[key] : el)) {
      newArr.push(replaceEmpty(key ? el[key] : el, ''))
    }
  })

  return newArr.join(separatrix) || replace
}

export const handleJSONtoObj = (str: string, replace = {}): any => {
  let result = replace
  try {
    result = JSON.parse(str)
  } catch(err) {
    result = replace
  }

  return result
}
